/*
 * Copyright 2008-2009 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public Licence Version 3
 * The licence can be read in its entirety in the LICENSE.txt file accompaning this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of WeightWatch.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package net.cachapa.weightwatch.ui;

import com.sun.lwuit.ComboBox;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import java.util.Vector;
import net.cachapa.weightwatch.Preferences;
import net.cachapa.weightwatch.util.L;

public class LocalizationPreferencesContainer extends Container implements PreferencesContainer, ActionListener {
	private Application application;
	private String name;
	private Image icon;
	private ComboBox languageBox;
	private ComboBox unitsBox;
	private ComboBox energyBox;

	public LocalizationPreferencesContainer(Application application) {
		this.application = application;
		this.setScrollableY(true);
		this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
		this.name = L.l("pref_tab_localization");
		try {
			this.icon = Image.createImage("/localization.png");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		Preferences prefs = application.getWeightWatch().getPreferences();
		
		this.addComponent(new Label("localization_language"));
		Vector availableLanguages = application.getAvailableLanguages();
		int langIndex = availableLanguages.indexOf(prefs.getLanguage());
		if (langIndex < 0 || langIndex >= availableLanguages.size()) langIndex = 0;
		languageBox = new ComboBox(availableLanguages);
		languageBox.setSelectedIndex(langIndex);
		languageBox.addActionListener(this);
		this.addComponent(languageBox);
		
		// meters or inches
		this.addComponent(new Label("localization_unitsystem"));
		unitsBox = new ComboBox(new String[]{L.l("localization_metric"), L.l("localization_imperial")});
		if (prefs.isMetricUnits()) {
			unitsBox.setSelectedIndex(0, true);
		}
		else {
			unitsBox.setSelectedIndex(1, true);
		}
		unitsBox.addActionListener(this);
		this.addComponent(unitsBox);
		
//		// kcals or kj
//		this.addComponent(new Label("localization_heightunit"));
//		heightBox = new ComboBox(new String[]{"masd"});
////		heightBox.setSelectedItem("m");
//		this.addComponent(heightBox);
	}
	
	public void show() {
		
	}

	public void actionPerformed(ActionEvent ae) {
		Preferences prefs = application.getWeightWatch().getPreferences();
		Object source = ae.getSource();
		if (source == languageBox) {
			String selectedLanguage = (String)languageBox.getSelectedItem();
			application.setLanguage(selectedLanguage);
			prefs.setLanguage(selectedLanguage);
			Dialog.show("localization_changetitle", "localization_changemessage", "ok", "");
		}
		
		else if (source == unitsBox) {
			if (unitsBox.getSelectedIndex() == 0) {
				prefs.setMetricUnits(true);
			}
			else {
				prefs.setMetricUnits(false);
			}
		}
	}

	public String getName() {
		return name;
	}

	public Image getIcon() {
		return icon;
	}
}
